package com.feihong.yw189.popupwindow;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.feihong.yw189.R;
import com.feihong.yw189.activity.kaika.SelectNumActivity;
import com.feihong.yw189.app.App;
import com.feihong.yw189.bluetooth.utils.util.LogUtils;
import com.feihong.yw189.bluetooth.utils.util.PrefUtils;
import com.feihong.yw189.popupwindow.listener.BackPopMapListener;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.HashMap;
import java.util.Map;

public class ShowPopupWindow {
    private BackPopMapListener backListener;
    // 号段
    //private String[] dxHds = new String[]{"133", "153", "177", "180", "181", "189", "199"};
    private String[] dxHds = new String[]{"全部","133", "153", "173","177", "180", "181", "189", "199"};

    // 号等级

    private String[] dxDjs = new String[]{"全部","0级", "1级", "2级", "3级", "4级", "5级", "6级", "7级", "8级"};

    // 低消
    private String[] dxDxs = new String[]{"全部","0元","49元", "89元", "129元", "189元", "229元", "389元"};

    // 预存
    private String[] dxYcs = new String[]{"全部","0元","200元", "400元", "800元", "1500元", "3000元", "6000元", "12000元"};
    private String[] mVals = new String[]{};

    private int hmType = 1;
    private Activity mActivity;
    private TagFlowLayout mFlowLayout;
    private TextView tv_finish;

    public ShowPopupWindow(Activity activity, BackPopMapListener backListener) {
        this.mActivity = activity;
        this.backListener = backListener;

        PrefUtils.setInt(mActivity, "HAODUAN", 0);
        PrefUtils.setInt(mActivity, "DENGJI", 0);
        PrefUtils.setInt(mActivity, "DIXIAO", 0);
        PrefUtils.setInt(mActivity, "YUCUN", 0);
    }

    public void setData(int hmType) {
        this.hmType = hmType;
        switch (hmType) {  // 点击哪个按钮打开popupwindow
            case 1: // 号段
                mVals = dxHds;
                break;
            case 2: // 等级
                mVals = dxDjs;
                break;
            case 3: // 低消
                mVals = dxDxs;
                break;
            case 4: // 预存
                mVals = dxYcs;
                break;
                default:break;
        }
        setMyAdapter();
        mFlowLayout.getAdapter().notifyDataChanged();
    }

    public void setMyAdapter() {
        int haoDuan = PrefUtils.getInt(mActivity, "HAODUAN", 0);
        int dengJi = PrefUtils.getInt(mActivity, "DENGJI", 0);
        int diXiao = PrefUtils.getInt(mActivity, "DIXIAO", 0);
        int yuCun = PrefUtils.getInt(mActivity, "YUCUN", 0);

        mFlowLayout.setAdapter(new TagAdapter<String>(mVals) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(mActivity).inflate(R.layout.popu_tv,
                        mFlowLayout, false);
                tv.setText(s);
                return tv;
            }
        });
        // 设置默认选中
        switch (hmType) {
            case 1: // 号码类型
                mFlowLayout.getAdapter().setSelectedList(haoDuan);
                break;
            case 2: // 号码段
                mFlowLayout.getAdapter().setSelectedList(dengJi);
                break;

            case 3: // 靓号类型
                mFlowLayout.getAdapter().setSelectedList(diXiao);
                break;
            case 4: // 靓号费
                mFlowLayout.getAdapter().setSelectedList(yuCun);
                break;
                default:break;
        }


    }

    @TargetApi(Build.VERSION_CODES.DONUT)
    @RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
    public PopupWindow showPopupWindow(final ChangeArrowListener changeArrowListener) {
        View convertView = LayoutInflater.from(mActivity).inflate(R.layout.popupwindow, null);
        final PopupWindow popupWindow = new PopupWindow(convertView);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        ColorDrawable dw = new ColorDrawable();
        popupWindow.setBackgroundDrawable(dw);
        popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mFlowLayout = (TagFlowLayout) convertView.findViewById(R.id.flowlayout);
        popupWindow.setClippingEnabled(true);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                changeArrowListener.changeArrowListener();
            }
        });
//        tv_finish = (TextView) convertView.findViewById(R.id.tv_finish);

//        tv_finish.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                popupWindow.dismiss();
//                backListener.backPopMap();
//            }
//        });

        // 点击之后更新选中值
        mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                String mHd = "";
                String mDj = "";
                String mDx = "";
                String mYc = "";

                switch (hmType) {
                    case 1: // 号段
                        PrefUtils.setInt(mActivity, "HAODUAN", position);
                        selectDataType(SELECT_PHONENUM_HAODUAN,dxHds[position]);
                        break;
                    case 2: // 等级
                        PrefUtils.setInt(mActivity, "DENGJI", position);
                        String DJValue = dxDjs[position];
                        if(position != 0){
                            DJValue = DJValue.substring(0,DJValue.length() - 1);
                        }
                        selectDataType(SELECT_PHONENUM_DENGJI,DJValue);
                        break;

                    case 3: // 低消
                        PrefUtils.setInt(mActivity, "DIXIAO", position);
                        String DXValue = dxDxs[position];
                        if(position != 0){
                            DXValue = DXValue.substring(0,DXValue.length() - 1);
                        }
                        selectDataType(SELECT_PHONENUM_DIXIAO,DXValue);
                        break;
                    case 4: // 预存
                        PrefUtils.setInt(mActivity, "YUCUN", position);
                        String YCValue = dxYcs[position];
                        if(position != 0){
                            YCValue = YCValue.substring(0,YCValue.length() - 1);
                        }
                        selectDataType(SELECT_PHONENUM_YUCUN,YCValue);
                        break;
                        default:break;

                }
                backListener.backPopMap();
                return true;
            }

        });
        return popupWindow;
    }

    /**
     * 弹窗消失 手动修改箭头方向
     */
    public interface ChangeArrowListener{
        void changeArrowListener();
    }

    public interface AddPhoneNumTypeListener{
        /**
         *
         * @param phoneNumType 手机号码类型
         */
        void addPhoneNumTypeListener(Map<String,String> phoneNumType);
    }

    private AddPhoneNumTypeListener mAddPhoneNumTypeListener;
    public void addPhoneNumTypeListener(AddPhoneNumTypeListener addPhoneNumTypeListener){
        mAddPhoneNumTypeListener = addPhoneNumTypeListener;
    }
    private Map<String,String> mData = new HashMap<>();
    public final static String SELECT_PHONENUM_YUCUN = "price";
    public final static String SELECT_PHONENUM_HAODUAN = "duan";
    public final static String SELECT_PHONENUM_DENGJI = "dengji";
    public final static String SELECT_PHONENUM_DIXIAO = "mini_cost";
    private void selectDataType(String key,String value){
        mData.clear();
        mData.put(key,value);
        mAddPhoneNumTypeListener.addPhoneNumTypeListener(mData);
    }

}
